package src.web.servlet;

import src.domain.Account;
import src.domain.Cart;
import src.service.CartService;
import src.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

public class UpdateCartServlet extends HttpServlet {

    private static final String CART_FORM_SERVLET = "cartForm";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account account = (Account)session.getAttribute("loginAccount");
        if (account != null) {
            Cart cart = (Cart)session.getAttribute("cart");
            if (cart != null) {
                for (int i = 0; i < cart.getItemList().size(); i++) {
                    String itemId = cart.getItemList().get(i).getItemId();
                    int quantity;
                    try {
                        quantity = Integer.parseInt(req.getParameter(itemId));
                    } catch (NumberFormatException ex) {
                        System.out.println("[UpdateCartServlet] 格式转换错误: 商品数量含有非法字符");
                        continue;
                    }
                    cart.setQuantity(itemId,quantity);
                }
                CartService cartService = new CartService();
                cartService.updateCart(account,cart);
            }
        }
        resp.sendRedirect(CART_FORM_SERVLET);
    }

    //使用get方法则只更新特定的item的数据(当且仅当cart.js发送AJAX请求时用,提高效率)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String itemId = req.getParameter("qtyChangedItemId");
        Account account = (Account)session.getAttribute("loginAccount");
        int quantity = -1;
        try {
            quantity = Integer.parseInt(req.getParameter("changedQty"));
        } catch (NumberFormatException ex) {
            System.out.println("[UpdateCartServlet] 格式转换错误: 商品数量含有非法字符");
            return;
        }
        if (account != null && itemId != null) {
            CartService cartService = new CartService();
            cartService.updateItemQtyInCart(account, itemId, quantity);
//            Cart cart = (Cart)session.getAttribute("cart");
            CatalogService catalogService = new CatalogService();
            BigDecimal totalCost = catalogService.getItem(itemId).getListPrice().multiply(new BigDecimal(Integer.valueOf(quantity)));
            Cart cart = new CartService().getCartByAccount(account);
            cart.getTotalPrice();
            //服务器返回当前购物车改变的item的总价格和购物车所有item的总价格,用'_'隔开
            String respStr = totalCost + "_" + cart.getTotalPrice();
            resp.setContentType("text/plain");
            PrintWriter printWriter = resp.getWriter();
            printWriter.print(respStr);
        }

    }
}
